function [path_loss_results, fig_path_loss] = path_loss_models(distance_range, carrier_freq)
% 路径损耗模型分析
% 输入参数：
%   distance_range - 距离范围 (米)
%   carrier_freq - 载波频率 (Hz)
% 输出参数：
%   path_loss_results - 路径损耗结果
%   fig_path_loss - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

%% 并行计算设置
[use_parallel, pool_info] = parallel_manager();

% 参数设置
speed_of_light = 3e8; % 光速
h_tx = 30; % 发射天线高度 30m
h_rx = 1.5; % 接收天线高度 1.5m

% 初始化结果
path_loss_results = struct();
path_loss_results.distance = distance_range;
path_loss_results.carrier_freq = carrier_freq;

if use_parallel
    % 使用并行计算计算路径损耗模型
    fprintf('使用并行计算计算路径损耗模型...\n');
    
    % 预计算自由空间路径损耗（供其他模型使用）
    fspl_temp = 20*log10(distance_range) + 20*log10(carrier_freq) + 20*log10(4*pi/speed_of_light);
    
    % 预计算参考损耗（供后续使用）
    reference_loss = fspl_temp(1);
    
    % 并行计算各种路径损耗模型
    temp_results = cell(5, 1); % 存储5种模型的结果
    
    parfor i = 1:5
        switch i
            case 1
                % 1. 自由空间路径损耗 (FSPL)
                temp_results{i} = fspl_temp;
            case 2
                % 2. 对数距离路径损耗模型
                n = 3.5; % 路径损耗指数 (城市环境)
                reference_distance = 1; % 参考距离 1m
                temp_results{i} = reference_loss + 10*n*log10(distance_range/reference_distance);
            case 3
                % 3. Hata模型 (城市环境)
                temp_results{i} = 69.55 + 26.16*log10(carrier_freq/1e6) - 13.82*log10(h_tx) - ...
                    (1.1*log10(carrier_freq/1e6) - 0.7)*h_rx + 1.56*log10(carrier_freq/1e6) + ...
                    44.9 - 6.55*log10(h_tx) + log10(distance_range/1000);
            case 4
                % 4. COST-231 Hata模型 (扩展Hata模型)
                temp_results{i} = 46.3 + 33.9*log10(carrier_freq/1e6) - 13.82*log10(h_tx) - ...
                    (1.1*log10(carrier_freq/1e6) - 0.7)*h_rx + 1.56*log10(carrier_freq/1e6) + ...
                    44.9 - 6.55*log10(h_tx) + 3.2*log10(11.75*h_rx)^2 + log10(distance_range/1000);
            case 5
                % 5. 简化的室内模型
                n_indoor = 2.8; % 室内路径损耗指数
                reference_distance = 1; % 参考距离 1m
                temp_results{i} = reference_loss + 10*n_indoor*log10(distance_range/reference_distance) + 5; % 额外墙体损耗
        end
    end
    
    % 将临时结果复制到最终结构体
    path_loss_results.fspl = temp_results{1};
    path_loss_results.log_distance = temp_results{2};
    path_loss_results.hata = temp_results{3};
    path_loss_results.cost231_hata = temp_results{4};
    path_loss_results.indoor = temp_results{5};
else
    % 串行计算
    % 1. 自由空间路径损耗 (FSPL)
    path_loss_results.fspl = 20*log10(distance_range) + 20*log10(carrier_freq) + 20*log10(4*pi/speed_of_light);

    % 2. 对数距离路径损耗模型
    n = 3.5; % 路径损耗指数 (城市环境)
    reference_distance = 1; % 参考距离 1m
    reference_loss = path_loss_results.fspl(1); % 参考点损耗
    path_loss_results.log_distance = reference_loss + 10*n*log10(distance_range/reference_distance);

    % 3. Hata模型 (城市环境)
    path_loss_results.hata = 69.55 + 26.16*log10(carrier_freq/1e6) - 13.82*log10(h_tx) - ...
        (1.1*log10(carrier_freq/1e6) - 0.7)*h_rx + 1.56*log10(carrier_freq/1e6) + ...
        44.9 - 6.55*log10(h_tx) + log10(distance_range/1000);

    % 4. COST-231 Hata模型 (扩展Hata模型)
    path_loss_results.cost231_hata = 46.3 + 33.9*log10(carrier_freq/1e6) - 13.82*log10(h_tx) - ...
        (1.1*log10(carrier_freq/1e6) - 0.7)*h_rx + 1.56*log10(carrier_freq/1e6) + ...
        44.9 - 6.55*log10(h_tx) + 3.2*log10(11.75*h_rx)^2 + log10(distance_range/1000);

    % 5. 简化的室内模型
    n_indoor = 2.8; % 室内路径损耗指数
    path_loss_results.indoor = reference_loss + 10*n_indoor*log10(distance_range/reference_distance) + 5; % 额外墙体损耗
end

%% 可视化结果
fig_path_loss = figure('Name', '路径损耗模型对比', 'Position', [100, 100, 1200, 800]);

% 子图1: 路径损耗对比
subplot(2, 2, 1);
plot(distance_range, path_loss_results.fspl, 'LineWidth', 2, 'Color', colors(1, :));
hold on;
plot(distance_range, path_loss_results.log_distance, 'LineWidth', 2, 'Color', colors(2, :));
plot(distance_range, path_loss_results.hata, 'LineWidth', 2, 'Color', colors(3, :));
plot(distance_range, path_loss_results.cost231_hata, 'LineWidth', 2, 'Color', colors(4, :));
plot(distance_range, path_loss_results.indoor, 'LineWidth', 2, 'Color', colors(5, :));
grid on;
xlabel('距离 (m)');
ylabel('路径损耗 (dB)');
title('路径损耗模型对比');
legend({'自由空间模型', '对数距离模型', 'Hata模型', 'COST-231 Hata模型', '室内模型'}, 'Location', 'NorthWest');

% 子图2: 路径损耗指数影响
subplot(2, 2, 2);
n_values = [2, 2.5, 3, 3.5, 4];
reference_distance = 1; % 参考距离 1m

if use_parallel
    % 并行计算不同路径损耗指数
    fprintf('使用并行计算分析路径损耗指数影响...\n');
    parfor i = 1:length(n_values)
        pl_model_parallel(i).data = reference_loss + 10*n_values(i)*log10(distance_range/reference_distance);
    end
    
    % 绘制结果
    for i = 1:length(n_values)
        plot(distance_range, pl_model_parallel(i).data, 'LineWidth', 2, 'Color', colors(i, :));
        hold on;
    end
else
    % 串行计算
    for i = 1:length(n_values)
        pl_model = reference_loss + 10*n_values(i)*log10(distance_range/reference_distance);
        plot(distance_range, pl_model, 'LineWidth', 2, 'Color', colors(i, :));
        hold on;
    end
end
grid on;
xlabel('距离 (m)');
ylabel('路径损耗 (dB)');
title('路径损耗指数影响');
legend({'n=2 (自由空间)', 'n=2.5', 'n=3', 'n=3.5', 'n=4'}, 'Location', 'NorthWest');

% 子图3: 频率影响
subplot(2, 2, 3);
freqs = [900e6, 2.4e9, 5.8e9]; % 不同频段

if use_parallel
    % 并行计算不同频率的路径损耗
    fprintf('使用并行计算分析频率影响...\n');
    parfor i = 1:length(freqs)
        pl_freq_parallel(i).data = 20*log10(distance_range) + 20*log10(freqs(i)) + 20*log10(4*pi/speed_of_light);
    end
    
    % 绘制结果
    for i = 1:length(freqs)
        plot(distance_range, pl_freq_parallel(i).data, 'LineWidth', 2, 'Color', colors(i, :));
        hold on;
    end
else
    % 串行计算
    for i = 1:length(freqs)
        pl_freq = 20*log10(distance_range) + 20*log10(freqs(i)) + 20*log10(4*pi/speed_of_light);
        plot(distance_range, pl_freq, 'LineWidth', 2, 'Color', colors(i, :));
        hold on;
    end
end
grid on;
xlabel('距离 (m)');
ylabel('路径损耗 (dB)');
title('载波频率影响');
legend({'900 MHz', '2.4 GHz', '5.8 GHz'}, 'Location', 'NorthWest');

% 子图4: 接收功率预测
subplot(2, 2, 4);
tx_power_dbm = 20; % 发射功率 20 dBm
tx_power_db = tx_power_dbm - 30; % 转换为 dB
rx_power_fspl = tx_power_db - path_loss_results.fspl;
rx_power_log = tx_power_db - path_loss_results.log_distance;
rx_power_hata = tx_power_db - path_loss_results.hata;

plot(distance_range, rx_power_fspl, 'LineWidth', 2, 'Color', colors(1, :));
hold on;
plot(distance_range, rx_power_log, 'LineWidth', 2, 'Color', colors(2, :));
plot(distance_range, rx_power_hata, 'LineWidth', 2, 'Color', colors(3, :));
grid on;
xlabel('距离 (m)');
ylabel('接收功率 (dBm)');
title('接收功率预测 (Tx Power = 20 dBm)');
legend({'自由空间模型', '对数距离模型', 'Hata模型'}, 'Location', 'SouthWest');

fprintf('路径损耗模型分析完成！\n');

end